package io.yyxx.boot.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 获取宜搭应用的token
 */
@Component
public class getDingTalkToken {

    //从配置文件中获取应用appKey和secret并初始化
    @Value("${dingtalk.appkey}")
    private String appKey;

    @Value("${dingtalk.appsecret}")
    private String appsecret;

    public String getAccessToken() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.setAppkey(appKey);
            req.setAppsecret(appsecret);
            req.setHttpMethod("GET");
            OapiGettokenResponse rsp = client.execute(req);
            String accessToken = rsp.getAccessToken();
            return accessToken;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}
